package com.hz.springbootschedule.config;

import com.hz.springbootschedule.pojo.SmbmsUser;
import com.hz.springbootschedule.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author shkstart
 * @create 2023-02-13 18:13
 */
@Configuration
public class LoginInterceptor implements WebMvcConfigurer {
    @Autowired
    private RedisUtil redisUtil;

    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new HandlerInterceptor() {
          public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
              System.out.println("进入拦截器");
              String token =  request.getHeader("token");//获得token值
              if(token==null){
                  System.out.println(11);
                  return false;

              }
              Object object =  redisUtil.getObj("login:"+token);
              if(object==null){
                  System.out.println(22);
                  return false;
              }
                return true;
            }
        }).addPathPatterns("/**").excludePathPatterns("/index/login");

    }

}
